
/*
 * Copyright (C) Zhu Jiashun
 * Copyright (C) Zaver
 */

#ifndef ZV_LIST_H
#define ZV_LIST_H

#ifndef NULL
#define NULL 0
#endif


/**********************************************
 * list_head, reference linux kernel implementation
 * 一个参考了内核实现的 双向链表 数据结构
 **********************************************/
typedef struct list_head {
    struct list_head *prev, *next;
}list_head;


/**********************************************
 * list_head初始化宏
 **********************************************/
#define INIT_LIST_HEAD(ptr) do {\
    struct list_head *_ptr = (struct list_head *)ptr;   \
    (_ptr)->next = (_ptr); (_ptr->prev) = (_ptr);       \
} while(0)


/**********************************************
 * 将一个节点插入prev和next节点之间
 **********************************************/
static inline void __list_add(struct list_head *_new, struct list_head *prev, struct list_head *next) {
    _new->next = next;
    next->prev = _new;
    prev->next = _new;
    _new->prev = prev;
}


/**********************************************
 * 在头部插入一个节点
 **********************************************/
static inline void list_add(struct list_head *_new, struct list_head *head) {
    __list_add(_new, head, head->next);
}


/**********************************************
 * 在尾部插入一个节点
 **********************************************/
static inline void list_add_tail(struct list_head *_new, struct list_head *head) {
    __list_add(_new, head->prev, head);
}


/**********************************************
 * 删除两个节点之间的一个节点
 **********************************************/
static inline void __list_del(struct list_head *prev, struct list_head *next) {
    prev->next = next;
    next->prev = prev;
}


/**********************************************
 * 删除一个节点
 **********************************************/
static inline void list_del(struct list_head *entry) {
    __list_del(entry->prev, entry->next);
    //entry->next = entry->prev = NULL;
}


/**********************************************
 * 检查双向链表是否为空
 **********************************************/
static inline int list_empty(struct list_head *head) {
    return (head->next == head) && (head->prev == head);
}


/**********************************************
 * 求offset
 **********************************************/
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)


/**********************************************
 * 根据一个成员的地址，求出整个结构体的首地址
 **********************************************/
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );     \
})


/**********************************************
 * 返回列表节点的首地址
 **********************************************/
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)


/**********************************************
 * 用pos指针 向后 迭代遍历每一个节点
 **********************************************/
#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)


/**********************************************
 * 用pos指针 向前 迭代遍历每一个节点
 **********************************************/
#define list_for_each_prev(pos, head) \
    for (pos = (head)->prev; pos != (head); pos = pos->prev)

/*
#define list_for_each_entry(pos, head, member)  \
    for (pos = list_entry((head)->next, typeof(*pos), member);  \
        &(pos->member) != (head);                               \
        pos = list_entry(pos->member.next, typeof(*pos), member))
*/

#endif 
